<?php

/**
 * Good_material_model short summary.
 *
 * Good_material_model description.
 *
 * @version 1.0
 * @author GALAXY
 */
class Good_material_model extends CI_Model
{
    public $good_material_id;
    public $title;
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }

    //插入新材质
    public function insert_good_material($good_materialModel)
    {
        return $this->db->insert('good_material',$good_materialModel);
    }

    //由材质id 获取材质
    public function get_good_material_by_id($good_material_id)
    {
        return $this->db->select()->from('good_material')->where(array('good_material_id'=>$good_material_id))->get()->row();
    }

    public function get_title($good_material_id)
    {
        return $this->db->select('title')->from('good_material')->where(array('good_material_id'=>$good_material_id))->get()->row();
    }
    //获取所有材质
    public function get_allgood_material()
    {
        return $this->db->select()->from('good_material')->get()->result();
    }



    //更新材质
    public function edit_good_material($good_materialModel)
    {

       return  $this->db->update('good_material',$good_materialModel,array('good_material_id'=>$good_materialModel->good_material_id));

    }

    //删除材质
    public function del_good_material_byid($good_material_id)
    {
        return $this->db->delete('good_material',array('good_material_id'=>$good_material_id));
    }

    //批量删除材质
    public function batchdel_good_material($good_material_ids)
    {
        $this->db->trans_begin();
        foreach($good_material_ids as $good_materialiditem)
        {
            $this->db->delete('good_material',array('good_material_id'=>$good_materialiditem));
        }
        if ($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return 0;
        }
        else
        {
            $this->db->trans_commit();
            return 1;
        }
    }



    /**
     * @param mixed $cpage
     * @param mixed $pagesize
     * @param mixed $files
     * @param mixed $tabel
     * @param mixed $order
     */
    public function get_page($cpage,$pagesize,$files,$tabel,$order,$temp_id,$where)
    {
        $sql="SELECT $files FROM $tabel a JOIN (select $temp_id from $tabel $where limit ".($cpage-1)*$pagesize.", $pagesize) b ON a.$temp_id = b.$temp_id ";
        $sql_count="select count('$temp_id') as _count from $tabel $where";
        $data['objlist']=$this->db->query($sql)->result();
        $data['count']=$this->db->query($sql_count)->result();
        return $data;
    }
}